package com.tegtech.module.dict;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.tegtech.common.constant.CacheExpire;
import com.tegtech.common.constant.CacheKey;
import com.tegtech.module.dict.domain.DictVal;
import com.tegtech.module.dict.mapper.DictValMapper;
import com.tegtech.starter.redis.service.RedisCacheHelper;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;

@Component
public class DictValCache {

	@Autowired
	private DictValMapper dictValMapper;
	
	/**
	 * 根据字典类型获取字典显示数据
	 * @param dictType
	 * @return
	 */
	public List<DictVal> findDictData(String dictType) {
		return RedisCacheHelper.hValue(CacheKey.SYS_DICT_KEY, dictType, CacheExpire.SEVEN_DAY, () -> dictValMapper.findDictValList(dictType));
	}
	
	/**
	 * 清除
	 * @param dictType
	 */
	public void clear(String dictType) {
		if(StrUtil.isNotBlank(dictType)) {
			RedisCacheHelper.instance.hdel(CacheKey.SYS_DICT_KEY, dictType);
		}
	}
	
	/**
	 * 清除
	 * @param dictTypes
	 */
	public void clear(List<String> dictTypes) {
		if(CollectionUtil.isNotEmpty(dictTypes)) {
			RedisCacheHelper.instance.hdel(CacheKey.SYS_DICT_KEY, dictTypes.toArray());
		}
	}
	
	/**
	 * 清除
	 * @param dictCodes
	 */
	public void clear(Long ... dictCodes) {
		if(dictCodes != null && dictCodes.length > 0) {
			List<String> dictTypes = dictValMapper.selectDictTypeByIds(dictCodes);
			RedisCacheHelper.instance.hdel(CacheKey.SYS_DICT_KEY, dictTypes.toArray());
		}
	}
	
	/**
	 * 清除所有字典缓存
	 */
	public void clearAll() {
		RedisCacheHelper.instance.del(CacheKey.SYS_DICT_KEY);
	}
	
}
